package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.WfNodeRole;
import com.yu.crm.domain.request.NodeRoleUserReq;
import com.yu.crm.mapper.WfNodeRoleMapper;
import com.yu.crm.service.CrmUserRoleService;
import com.yu.crm.service.WfNodeRoleService;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工作流节点角色关联表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-10-08
 */
@Service
public class WfNodeRoleServiceImpl extends ServiceImpl<WfNodeRoleMapper, WfNodeRole> implements WfNodeRoleService {

    @Autowired
    private CrmUserRoleService crmUserRoleService;
    /**
     * 判断当前登录人是否具有审批权限
     */
    @Override
    public boolean isAuthor(Long nodeId, String userId) {
        //查询该用户角色
        List<WfNodeRole> nodeRoleList = baseMapper.nodeRoleByUserId(userId,nodeId);
        if (DataHelpers.isEmpty(nodeRoleList) || nodeRoleList.size() == 0) {
            return false;
        }
        return true;
    }

    /**
     * 新增节点角色用户
     * @param id
     * @param nodeRoleUserReqList
     * @param appId
     */
    @Override
    public void saveNodeRole(String id, List<NodeRoleUserReq> nodeRoleUserReqList, String appId) {
        List<WfNodeRole> list = new ArrayList<>();
        for (NodeRoleUserReq nodeRoleUserReq:nodeRoleUserReqList
             ) {
            WfNodeRole wfNodeRole = new WfNodeRole();
            wfNodeRole.setNodeId(id);
            wfNodeRole.setRoleId(nodeRoleUserReq.getRoleId());
            wfNodeRole.setDealUserAll(nodeRoleUserReq.getDealUserAll());
            wfNodeRole.setGmtCreate(DateHelpers.now());
            wfNodeRole.setIsDeleted(0);
            wfNodeRole.setAppId(appId);
            list.add(wfNodeRole);
        }
        if (list.size()>0) {
            this.saveBatch(list);
        }
    }

    /**
     * 通过应用id做删除
     * @param appId
     */
    @Override
    public void del(String appId) {
        QueryWrapper<WfNodeRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        baseMapper.delete(queryWrapper);
    }
}
